//
//  VisitorLoginView.m
//  weibo-OC
//
//  Created by diyuyma on 16/2/28.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "VisitorLoginView.h"

@interface VisitorLoginView ()

@property (nonatomic, strong)UIImageView *circleView;
@property (nonatomic, strong)UIImageView *iconView;
@property (nonatomic, strong)UIImageView *backView;
@property (nonatomic, strong)UILabel *tipLabel;
@property (nonatomic, strong)UIButton *registerBtn;
@property (nonatomic, strong)UIButton *loginBtn;

@end

@implementation VisitorLoginView
//声明代理属性
//属性默认是强引用  需要添加weak
- (void)loginDidBtnClick {
    NSLog(@"loginDidBtnClick");
    if ([self.visitorDelegate respondsToSelector:@selector(visitorWillLogin)]) {
        //代理调用协议方法
        [self.visitorDelegate visitorWillLogin];
    }
}

- (void)registerBtnDidClick {
    NSLog(@"registerBtnDidClick");
    if ([self.visitorDelegate respondsToSelector:@selector(visitorWillRegister)]) {
        //代理调用协议方法
        [self.visitorDelegate visitorWillRegister];
    }
}

//MARK: 设置页面信息
- (void)setUIInfo: (NSString *)imageNane WithTitle: (NSString *)title {
    
    self.tipLabel.text = title;
    self.iconView.hidden = false;
    if (imageNane != nil) {
        self.circleView.image = [UIImage imageNamed:imageNane];
        [self bringSubviewToFront:self.circleView];
        self.iconView.hidden = true;
    } else {
        [self startAnimation];
    }
}

- (void)startAnimation {
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.repeatCount = MAXFLOAT;
    anim.toValue = @(2 * M_PI);
    anim.duration = 20;
    // 当动画结束 或者视图处于非活跃状态 动画不移除图层
    anim.removedOnCompletion = false;
    [self.circleView.layer addAnimation:anim forKey:nil];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:CGRectZero];
    
    [self setupUI];
    
    return self;
}

- (void)setupUI {
    
    [self addSubview:self.circleView];
    [self addSubview:self.iconView];
    [self addSubview:self.backView];
//
    [self addSubview:self.tipLabel];
    [self addSubview:self.loginBtn];
    [self addSubview:self.registerBtn];
    
    for (UIView *v in self.subviews) {
        v.translatesAutoresizingMaskIntoConstraints = false;
    }

    // 设置布局
    //VFL
    //"view1.attr1 = view2.attr2 * multiplier + constant"
    // 注意:布局的时候 尽量相对一个控件
    // 圆圈的束缚
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.circleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.circleView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-60]];
    // 大头标的束缚
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    //设置提示文案的约束
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    //设置宽度约束
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:224]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50]];
    //设置登录按钮的约束
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tipLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tipLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:35]];
//    //设置注册按钮的约束
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.tipLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tipLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:35]];
    // 设置背景视图的约束
    // 位移枚举 || []
    // 设置背景视图的约束 [String : NSUumber]
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[backView]-0-|" options:0 metrics:nil views:[NSDictionary dictionaryWithObjectsAndKeys:self.backView, @"backView", nil]]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[backView]-(-45)-[registerBtn]" options:0 metrics:nil views:[NSDictionary dictionaryWithObjectsAndKeys:self.backView, @"backView", self.registerBtn, @"registerBtn", nil]]];

//     设置背景颜色
    self.backgroundColor = [UIColor colorWithWhite:0.93 alpha:1];
    
//     添加点击事件
    [self.loginBtn addTarget:self action:@selector(loginDidBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.registerBtn addTarget:self action:@selector(registerBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
}

- (UIImageView *)circleView {
    if (_circleView == nil) {
        UIImageView* cir = [[UIImageView alloc] init];
        cir.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
        
        //        _circleView = [cir initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
        _circleView = cir;
        [_circleView sizeToFit];
    }
    
    return _circleView;
}

- (UIImageView *)iconView {
    if (_iconView == nil) {
        UIImageView* icon = [[UIImageView alloc] init];
        icon.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
        _iconView = icon;
        [_iconView sizeToFit];
    }
    
    return _iconView;
}

- (UIImageView *)backView {
    if (_backView == nil) {
        UIImageView* back = [[UIImageView alloc] init];
        back.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
        _backView = back;
        [_backView sizeToFit];
    }
    
    return _backView;
}

- (UILabel *)tipLabel {
    if (_tipLabel == nil) {
        UILabel *tip = [[UILabel alloc] init];
        [tip initWithLabel:@"关注一些人，回这里看看有什么,惊喜关注一些人，回这里看看有什么惊喜" WithColor:[UIColor lightGrayColor] WithSize:13 WithMargin:0];
        _tipLabel = tip;
    }
    
    return _tipLabel;
}

- (UIButton *)registerBtn {
    
    if (_registerBtn == nil) {
        
        UIButton *reg = [[UIButton alloc] init];
        [reg initWithTitle:@"登陆" WithBackImage:@"common_button_white_disable" WithColor:[UIColor lightGrayColor] WithImage:nil WithSize:0];
        _registerBtn = reg;
    }
    
    return _registerBtn;
}

- (UIButton *)loginBtn {
    
    if (_loginBtn == nil) {
        
        UIButton *log = [[UIButton alloc] init];
        [log initWithTitle:@"注册" WithBackImage:@"common_button_white_disable" WithColor:[UIColor orangeColor] WithImage:nil WithSize:0];
        _loginBtn = log;
    }
    
    return _loginBtn;
}


@end
